package org.csu.mypetstore_seller.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String url = request.getRequestURI();

        // 如果是登录页面，放行
        if (url.contains("/login")) {
            return true;
        }

        // 验证session中是否有用户信息
        if (session.getAttribute("seller") != null) {
            return true;
        }

        // 没有登录，重定向到登录页面
        response.sendRedirect("/login");
        return false;
    }
}
